package com.e2oto.seller.modules.seller.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.e2oto.seller.R;
import com.e2oto.seller.common.base.RecycleViewAdapter;
import com.e2oto.seller.common.base.RecycleViewHolder;
import com.e2oto.seller.common.utils.ImageLoader;
import com.e2oto.seller.modules.seller.bean.GoodsCategoryBean;
import java.util.List;

/**
 * Created by ZhangLiQiang on 2017/11/28.
 */

public class GoodsManageContentAdapter extends RecycleViewAdapter<GoodsCategoryBean.GoodsBean> {

    private Context context;
    private boolean isVisiable = false;
    private OnContentClick onContentClick;

    public GoodsManageContentAdapter(List<GoodsCategoryBean.GoodsBean> dataList, Context context) {
        super(dataList);
        this.context = context;
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_goods_manage_content;
    }

    @Override
    public void convert(final RecycleViewHolder holder, final GoodsCategoryBean.GoodsBean goodsBean, final int position) {

        View root = holder.getView(R.id.goods_content);
        ImageView logo = (ImageView) holder.getView(R.id.goods_iv);
        TextView name = (TextView) holder.getView(R.id.goods_name);
        TextView oldPrice = (TextView) holder.getView(R.id.goods_old_price);
        TextView price = (TextView) holder.getView(R.id.goods_price);
        TextView stock = (TextView) holder.getView(R.id.goods_stock);
        TextView status = (TextView) holder.getView(R.id.goods_status);
        ImageView isSelected = (ImageView) holder.getView(R.id.goods_is_selected);
        ImageView delete = (ImageView) holder.getView(R.id.goods_delete);

        //控制是否显示
        if (goodsBean.isShowSelected()) {
            isSelected.setVisibility(View.VISIBLE);
        } else {
            isSelected.setVisibility(View.GONE);
        }
        //控制是否选中
        if (goodsBean.isSelected()) {
            isSelected.setSelected(true);
        } else {
            isSelected.setSelected(false);
        }
        ImageLoader.load(context, goodsBean.getCover(), logo);
        name.setText(goodsBean.getName());
        oldPrice.setText("市场价: "+goodsBean.getMarket_price());
        price.setText("¥ "+goodsBean.getSell_price());
        stock.setText("库存: "+goodsBean.getStore_nums() + "");

        if (onContentClick != null) {
            delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onContentClick.onClickDelete(holder, goodsBean, position);
                }
            });
            isSelected.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onContentClick.onClickSelected(holder, goodsBean, position);
                }
            });
            root.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onContentClick.onClickRoot(holder, goodsBean, position);
                }
            });
        }

    }

    /***
     * 点了批量操作就切换状态
     */
    public void resetVisibility() {
        isVisiable = !isVisiable;
        for (GoodsCategoryBean.GoodsBean g : dataList) {
            g.setShowSelected(isVisiable);
        }
        notifyDataSetChanged();
    }

    /***
     * 点左边种类的时候要清空选中状态并且隐藏
     */
    public void resetSelectedStatus() {
        for (GoodsCategoryBean.GoodsBean g : dataList) {
            g.setShowSelected(false);
            g.setSelected(false);
        }
        notifyDataSetChanged();
    }

    public void setOnContentClick(OnContentClick onContentClick) {
        this.onContentClick = onContentClick;
    }

    public interface OnContentClick {
        void onClickDelete(RecycleViewHolder holder, GoodsCategoryBean.GoodsBean goodsBean, int position);

        void onClickSelected(RecycleViewHolder holder, GoodsCategoryBean.GoodsBean goodsBean, int position);

        void onClickRoot(RecycleViewHolder holder, GoodsCategoryBean.GoodsBean goodsBean, int position);
    }
}
